<%--
  document: cart-detail.jsp
  
  description:
    Display all user purchases.
 --%>

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%-- Importations --%>
<%@page import="javax.faces.context.*" %>
<%@page import="javax.el.*" %>
<%@page import="com.reimscopie.*" %>
<%@page import="com.reimscopie.controlers.*" %>
<%@page import="javax.naming.*" %>
<%@page import="javax.transaction.*" %>
<%@page import="com.reimscopie.beans.interfaces.* "%>

<%-- Tag libs --%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="core"%> 
<%@taglib uri="http://java.sun.com/jsf/html" prefix="html"%> 

<%-- Controlers --%>
<%@ include file="jspf/controlers.jspf" %>

<%-- i18n --%>
<%@include file="jspf/i18n/i18n.jspf" %>

<%
//    Purchase[] purchases = new Purchase[3] ;
//    purchases[0] = new Purchase(new SimpleProduct("nom1","description1","picture",true,10F,30),30) ;
//    purchases[1] = new Purchase(new SimpleProduct("nom2","description2","picture",true,10F,30),30) ;
//    purchases[2] = new Purchase(new SimpleProduct("nom3","description3","picture",true,10F,30),30) ;
    
    Purchase[] purchases = userControler.getCart() ;
%>

<%
    Context context = new InitialContext() ;
    String name ="com.reimscopie.beans.session.ReimsCopieUserBean_" +
    ReimsCopieUserLocal.class.getName()+"@Local" ;    
    ReimsCopieUserLocal bean = (ReimsCopieUserLocal)context.lookup(name) ;   
    
    String transactionName = "javax.transaction.UserTransaction" ;
    UserTransaction transaction =
            (UserTransaction)context.lookup(transactionName);
    transaction.begin() ;
    
    BonPlan bestPlan = bean.getBestBonPlan() ;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">

<html>
	<head>
    <%@include file = "jspf/header.jspf" %>
	</head>
  
  <body>

<core:view>
  
  <div id="page">
  
  <%@include file="jspf/top.jspf" %>
  
  <div id="middle">
    <div id="menu">
      <div id="search">
        <%@ include file = "jspf/search.jspf" %>
      </div>
      <div id="menu_task">
        <%@ include file = "jspf/home/menu.jspf" %>
      </div>
    </div>
    <div id="contents">
      <div id="onglet">
        <%! String activeTab = "null"; %>
        <%@ include file = "jspf/onglet.jspf" %>
      </div>
      <div id="tree">
        &gt;&gt; Panier
      </div>
      <div id="in">
<%
    out.print("<div class=\"hautgroscadre\">") ;
 
    if (purchases.length == 0)
      out.print("<br>Le panier est vide");
     else {
      out.print("<br>") ;
      out.print("<b> Votre panier </b>");
      out.print("</div>") ;
      out.print("<div class=\"milieugroscadre\">") ;
      out.print("<div class=\"text\">") ;
      out.print("<table border=\"0\">");
  	  out.print("<tr>") ;
  	  out.print("<th colspan=\"20\">Quantit&eacute;</th>") ;
  	  out.print("<th colspan=\"100\">Produit</th>") ;
  	  out.print("<th>Prix</th>") ;
	  out.print("</tr>") ;
	  
	  float totalCart = 0 ; // Prix d'une commande 
	  
      for (Purchase purchase : purchases) {
        Product product = purchase.getProduct() ;
        out.print("<tr>") ;
        out.print("<td colspan=\"20\" align=\"middle\">");
        out.print(purchase.getQuantity()) ;
        out.print("</td>");
        out.print("<td colspan=\"100\" align=\"middle\">");
        out.print(product.getName());
        out.print("</td>");
        out.print("<td align=\"middle\">");
        out.print(purchase.getTotalCost()+" euros") ;
        out.print("</td>");
        out.print("</tr>") ;
        totalCart=purchase.getTotalCost()+totalCart;
     }
     out.print("<tr>") ;
     out.print("<td colspan=\"20\" align=\"middle\">");
     out.print("</td>");
     out.print("<td colspan=\"100\" align=\"middle\">");
     out.print("Total");
     out.print("</td>");
     out.print("<td>");
     out.print(totalCart+" euros");
     out.print("</td>");
     out.print("</tr>") ;
     out.print("</table>") ;
%>
     </div>
<%
    }
%>

<%
        if (purchases.length != 0) {
%>
			<br>
	        <center>
<%    
  			if(!userControler.getConnexionstate()){
%>
					Vous devez vous connecter pour commander
					<br>
<%    
  			} else {
%>
		    	<html:form>
		        	<html:commandButton value="Commander" action="#{UserControler.orderCart}"/>
		 		</html:form>     
<%
			}
%>
				<br>
				<html:form>
					<html:commandButton value="Vider" action="#{UserControler.emptyCart}"/>
		 		</html:form>  
		    </center>
<%
		}
%>
    	</div>
		<div class="basgroscadre">
    	</div>    
      </div>
    </div>
  </div>
  
  <div id="page_end">
    <%@include file="jspf/page-end.jspf" %>
  </div>
  
</core:view>

<%@include file="jspf/footer.jspf" %>

<%
    transaction.commit();    
%>
